from django.db import models
from django.contrib.auth.models import AbstractUser
from django.utils import timezone


class User(AbstractUser):
    """用户模型"""
    # 移除last_login字段，避免与AbstractUser冲突
    # last_login = models.DateTimeField(auto_now_add=True, verbose_name='最后登录时间')
    phone = models.CharField(max_length=11, blank=True, null=True, verbose_name='手机号码')
    email = models.EmailField(blank=True, null=True, verbose_name='邮箱')
    address = models.CharField(max_length=255, blank=True, null=True, verbose_name='地址')
    role = models.CharField(max_length=20, choices=(("admin", "管理员"), ("user", "用户")), default="user", verbose_name='角色')
    
    class Meta:
        verbose_name = '用户'
        verbose_name_plural = '用户管理'
        db_table = 'users'
    
    def __str__(self):
        return self.username


class Room(models.Model):
    """房间模型"""
    ROOM_TYPE_CHOICES = [
        ('standard', '标准间'),
        ('deluxe', '豪华间'),
        ('suite', '套房'),
        ('family', '家庭房'),
        ('game', '电竞房'),
    ]
    
    STATUS_CHOICES = [
        ('available', '可用'),
        ('booked', '已预订'),
        ('maintenance', '维护中'),
    ]
    
    room_number = models.CharField(max_length=20, unique=True)
    name = models.CharField(max_length=100)
    description = models.TextField(null=True, blank=True)
    room_type = models.CharField(max_length=20, choices=ROOM_TYPE_CHOICES)
    price = models.DecimalField(max_digits=10, decimal_places=2)
    size = models.IntegerField(null=True, blank=True)
    capacity = models.IntegerField()
    status = models.CharField(max_length=20, choices=STATUS_CHOICES, default='available')
    count = models.IntegerField(default=0)  # 预订次数计数
    image_url = models.CharField(max_length=200, null=True, blank=True)
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)
    
    class Meta:
        db_table = 'rooms'
        
    def __str__(self):
        return f"{self.room_number} - {self.name}"


class Booking(models.Model):
    """订单模型"""
    STATUS_CHOICES = [
        ('pending', '待确认'),
        ('confirmed', '已确认'),
        ('checked_in', '已入住'),
        ('checked_out', '已退房'),
        ('cancelled', '已取消'),
    ]
    
    guest_name = models.CharField(max_length=100)
    guest_phone = models.CharField(max_length=20)
    guest_email = models.EmailField(null=True, blank=True)
    room = models.ForeignKey(Room, on_delete=models.CASCADE, related_name='bookings')
    user = models.ForeignKey(User, on_delete=models.CASCADE, null=True, blank=True, related_name='bookings')
    check_in = models.DateTimeField()
    check_out = models.DateTimeField()
    guests = models.IntegerField(default=1)
    total_price = models.DecimalField(max_digits=10, decimal_places=2)
    status = models.CharField(max_length=20, choices=STATUS_CHOICES, default='pending')
    notes = models.TextField(null=True, blank=True)
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)
    
    class Meta:
        db_table = 'bookings'
        
    def __str__(self):
        return f"Booking {self.id} - {self.guest_name}"


class Amenity(models.Model):
    """设施模型"""
    CATEGORY_CHOICES = [
        ('basic', '基础设施'),
        ('entertainment', '娱乐设施'),
        ('service', '服务设施'),
        ('safety', '安全设施'),
    ]
    
    name = models.CharField(max_length=50)
    category = models.CharField(max_length=30, choices=CATEGORY_CHOICES)
    icon = models.CharField(max_length=50, null=True, blank=True)
    description = models.TextField(null=True, blank=True)
    
    class Meta:
        db_table = 'amenities'
        
    def __str__(self):
        return self.name


class RoomAmenity(models.Model):
    """房间-设施关联模型"""
    room = models.ForeignKey(Room, on_delete=models.CASCADE)
    amenity = models.ForeignKey(Amenity, on_delete=models.CASCADE)
    
    class Meta:
        db_table = 'room_amenities'
        unique_together = ('room', 'amenity')


class Contact(models.Model):
    """联系信息模型"""
    name = models.CharField(max_length=100)
    email = models.EmailField()
    phone = models.CharField(max_length=20)
    subject = models.CharField(max_length=100)
    message = models.TextField()
    submitted_at = models.DateTimeField(auto_now_add=True)
    
    class Meta:
        db_table = 'contacts'
        
    def __str__(self):
        return f"{self.name} - {self.subject}"


class Customer(models.Model):
    """客户模型"""
    MEMBER_LEVEL_CHOICES = [
        ('ordinary', '普通会员'),
        ('silver', '银卡会员'),
        ('gold', '金卡会员'),
        ('platinum', '白金会员'),
    ]
    
    name = models.CharField(max_length=100)
    phone = models.CharField(max_length=20, unique=True)
    email = models.EmailField(unique=True, null=True, blank=True)
    member_level = models.CharField(max_length=20, choices=MEMBER_LEVEL_CHOICES, default='ordinary')
    total_stays = models.IntegerField(default=0)
    total_spent = models.DecimalField(max_digits=12, decimal_places=2, default=0)
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)
    
    class Meta:
        db_table = 'customers'
        
    def __str__(self):
        return f"{self.name} ({self.phone})"


class Bill(models.Model):
    """账单模型"""
    STATUS_CHOICES = [
        ('pending', '待支付'),
        ('paid', '已支付'),
        ('refunded', '已退款'),
    ]
    
    booking = models.ForeignKey(Booking, on_delete=models.CASCADE, related_name='bills')
    amount = models.DecimalField(max_digits=10, decimal_places=2)
    status = models.CharField(max_length=20, choices=STATUS_CHOICES, default='pending')
    paid_at = models.DateTimeField(null=True, blank=True)
    created_at = models.DateTimeField(auto_now_add=True)
    
    class Meta:
        db_table = 'bills'
        
    def __str__(self):
        return f"Bill {self.id} - {self.amount}"


class Feedback(models.Model):
    """反馈模型"""
    STATUS_CHOICES = [
        ('pending', '待处理'),
        ('processing', '处理中'),
        ('resolved', '已解决'),
        ('closed', '已关闭'),
    ]
    
    user = models.ForeignKey(User, on_delete=models.CASCADE, related_name='feedbacks')
    title = models.CharField(max_length=200)
    description = models.TextField()
    status = models.CharField(max_length=20, choices=STATUS_CHOICES, default='pending')
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)
    
    class Meta:
        db_table = 'feedbacks'
        
    def __str__(self):
        return f"{self.title} - {self.user.username}"
